/* ======================================================= */
/**
 * File : TrackDBAdapter.java Date : Jan 27, 2010 at 10:24:56 PM
 */
/* ======================================================= */
package fr.isima.data;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;
import fr.isima.model.Track;
import fr.isima.tools.FileUtils;

/* ======================================================= */
/**
 * @author daniel
 */
/* ======================================================= */
public class TrackProviderAdapter
{
    /** Debug Tag */
    private static final String TAG = "TrackProviderAdapter";

    private ContentResolver     contentResolver;

    /* ======================================================= */
    /**
     * @param contentResolver
     */
    /* ======================================================= */
    public TrackProviderAdapter(ContentResolver contentResolver)
    {
        this.contentResolver = contentResolver;
    }

    public Uri insertTrack(Track track)
    {
        ContentValues values = new ContentValues();
        values.put(IsiHikingProvider.TRACK_NAME, track.getName());
        values.put(IsiHikingProvider.TRACK_PATH, track.getPath());
        values.put(IsiHikingProvider.TRACK_DATE, track.getDate().getTime());
        values
                .put(IsiHikingProvider.TRACK_FREQ_REFRESH, track
                        .getFreqRefresh());
        values
                .put(IsiHikingProvider.TRACK_DIST_REFRESH, track
                        .getDistRefresh());

        Log.d(TAG, "Inserting " +  track.toString());
        return contentResolver.insert(IsiHikingProvider.CONTENT_URI, values);
    }

    public int deleteTrack(long id)
    {
        Track track = getTrack(id);
        if(track != null)
        {
          FileUtils.deleteFile(track.getPath());  
        }
        
        Uri uri = ContentUris.withAppendedId(IsiHikingProvider.CONTENT_URI, id);
        Log.d(TAG, "Deleting Track# " +  id);
        return contentResolver.delete(uri, null, null);
    }

    public int deleteAllTracks()
    {
        List<Track> tracks = getAllTracksSortedByName();
        for (Track track : tracks)
        {
            FileUtils.deleteFile(track.getPath());
        }
        
        Log.d(TAG, "Deleting all tracks ");
        return contentResolver
                .delete(IsiHikingProvider.CONTENT_URI, null, null);
    }

    public Track getTrack(long id)
    {
        Track res = null;

        Cursor cr = contentResolver.query(IsiHikingProvider.CONTENT_URI, null,
                IsiHikingProvider.TRACK_ID + "=" + id, null, null);

        if (cr.moveToFirst())
        {
            res = new Track(cr.getString(IsiHikingProvider.COLUMN_TRACK_ID), cr
                    .getString(IsiHikingProvider.COLUMN_TRACK_NAME), cr
                    .getString(IsiHikingProvider.COLUMN_TRACK_PATH), new Date(
                    cr.getLong(IsiHikingProvider.COLUMN_TRACK_DATE)), cr
                    .getInt(IsiHikingProvider.COLUMN_TRACK_FREQ_REFRESH), cr
                    .getInt(IsiHikingProvider.COLUMN_TRACK_DIST_REFRESH));
            Log.d(TAG, "Selecting Track# " +  id + " " + res.toString());
        }

        cr = null;

        return res;

    }
    
    public Track getTrackByName(String trackName)
    {
        Track res = null;

        Cursor cr = contentResolver.query(IsiHikingProvider.CONTENT_URI, null,
                IsiHikingProvider.TRACK_NAME + "=\"" + trackName + "\"", null, null);

        if (cr.moveToFirst())
        {
            res = new Track(cr.getString(IsiHikingProvider.COLUMN_TRACK_ID), cr
                    .getString(IsiHikingProvider.COLUMN_TRACK_NAME), cr
                    .getString(IsiHikingProvider.COLUMN_TRACK_PATH), new Date(
                    cr.getLong(IsiHikingProvider.COLUMN_TRACK_DATE)), cr
                    .getInt(IsiHikingProvider.COLUMN_TRACK_FREQ_REFRESH), cr
                    .getInt(IsiHikingProvider.COLUMN_TRACK_DIST_REFRESH));
            Log.d(TAG, "Selecting Track# " +  trackName + " " + res.toString());
        }

        cr = null;

        return res;

    }

    public List<Track> getAllTracksSortedByName()
    {
        Log.d(TAG, "Selecting all tracks sorted by name");
        return getAllTracksSorted(IsiHikingProvider.TRACK_NAME);
    }

    public List<Track> getAllTracksSortedByDate()
    {
        Log.d(TAG, "Selecting all tracks sorted by date");
        return getAllTracksSorted(IsiHikingProvider.TRACK_DATE);
    }

    private List<Track> getAllTracksSorted(String sortOrder)
    {
        List<Track> list = new ArrayList<Track>();

        Cursor cr = contentResolver.query(IsiHikingProvider.CONTENT_URI, null,
                null, null, sortOrder);

        if (cr.moveToFirst())
        {
            do
            {
                Track newTrack = new Track(cr
                        .getString(IsiHikingProvider.COLUMN_TRACK_ID), cr
                        .getString(IsiHikingProvider.COLUMN_TRACK_NAME), cr
                        .getString(IsiHikingProvider.COLUMN_TRACK_PATH),
                        new Date(cr
                                .getLong(IsiHikingProvider.COLUMN_TRACK_DATE)),
                        cr.getInt(IsiHikingProvider.COLUMN_TRACK_FREQ_REFRESH),
                        cr.getInt(IsiHikingProvider.COLUMN_TRACK_DIST_REFRESH));

                list.add(newTrack);
            }
            while (cr.moveToNext());
        }

        cr = null;

        return list;
    }    
}
